package com.wzx.ptplatform.controller.admin.student;

import com.wzx.ptplatform.domain.Student;
import com.wzx.ptplatform.domain.StudentMessage;
import com.wzx.ptplatform.service.StudentMessageService;
import com.wzx.ptplatform.service.StudentService;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员学生模块——学生消息管理")
@RestController
public class AdminStudentStudentMessageController {

    final StudentMessageService studentMessageService;
    final StudentService studentService;

    public AdminStudentStudentMessageController(StudentMessageService studentMessageService, StudentService studentService) {
        this.studentMessageService = studentMessageService;
        this.studentService = studentService;
    }

    @RequestMapping("admin_student_student_message_message_list")
    public LayuiPager<StudentMessage> adminStudentStudentMessageMessageList(
            String studentName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        StudentMessage studentMessage = new StudentMessage();
        if (studentName != null && !"".equals(studentName)) {
            studentMessage.setStudent(new Student(studentName));
        }
        Page<StudentMessage> pager = studentMessageService.findList(studentMessage, page - 1, limit);
        LayuiPager<StudentMessage> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_student_student_message_message_addView")
    public ModelAndView adminStudentStudentMessageMessageAddView() {
        return new ModelAndView("admin/student/student_message_message_add");
    }

    @GetMapping("admin_student_student_message_message_editView")
    public ModelAndView adminStudentStudentMessageEditView(String id, Model model) {
        model.addAttribute("studentMessage", studentMessageService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/student/student_message_message_edit");
    }

    @PostMapping("admin_student_student_message_message_save_or_edit")
    public Map<String, Object> adminStudentStudentMessageMessageSaveOrEdit(StudentMessage studentMessage) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (studentMessageService.saveOrEdit(studentMessage)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_student_student_message_message_delete")
    public Map<String, Object> adminStudentStudentMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (studentMessageService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_student_student_message_message_delete_select")
    public Map<String, Object> adminStudentStudentMessageDeleteSelect(String arrayId) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (arrayId != null && !"".equals(arrayId)) {
            String[] array = arrayId.split(",");
            for (String id : array) {
                if (id != null && !"".equals(id)) {
                    studentMessageService.deleteById(Long.parseLong(id));
                }
            }
            MapUtils.successMap(map);
        }
        return map;
    }
}
